<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="唯一键ID" prop="sysid">
        <el-input
          v-model="queryParams.sysid"
          placeholder="请输入唯一键ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="军事信息资源分类号" prop="miresnum">
        <el-input
          v-model="queryParams.miresnum"
          placeholder="请输入军事信息资源分类号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书名" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入书名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版地" prop="publicationplace">
        <el-input
          v-model="queryParams.publicationplace"
          placeholder="请输入出版地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版者" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题词" prop="subjectwords">
        <el-input
          v-model="queryParams.subjectwords"
          placeholder="请输入主题词"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要负责人" prop="mainresperson">
        <el-input
          v-model="queryParams.mainresperson"
          placeholder="请输入主要负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源制作者" prop="resourceproducer">
        <el-input
          v-model="queryParams.resourceproducer"
          placeholder="请输入资源制作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="metadataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <af-table-column label="唯一键ID" align="center" prop="sysid" />
      <af-table-column label="页码数" align="center" prop="pagenum" />
      <af-table-column label="军事信息资源分类号" align="center" prop="miresnum" />
      <af-table-column label="书名" align="center" prop="title" />
      <af-table-column label="版本信息" align="center" prop="versionInfor" />
      <af-table-column label="出版信息" align="center" prop="publishingInfor" />
      <af-table-column label="出版地" align="center" prop="publicationPlace" />
      <af-table-column label="出版者" align="center" prop="publisher" />
      <af-table-column label="主题词" align="center" prop="subjectWords" />
      <af-table-column label="主要负责人" align="center" prop="mainResPerson" />
      <af-table-column label="权限声明" align="center" prop="perStatement" />
      <af-table-column label="资源制作者" align="center" prop="resourceProducer" />
      <af-table-column label="正文语种" align="center" prop="textLanguage" />
      <af-table-column label="出版日期" align="center" prop="publicationDate" />
      <af-table-column label="摘要" align="center" prop="abstracts" />
      <af-table-column label="关键词" align="center" prop="keyWord" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人民工兵元数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="唯一键ID" prop="sysid">
          <el-input v-model="form.sysid" placeholder="请输入唯一键ID" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="页码数" prop="pagenum">
          <el-input v-model="form.pagenum" placeholder="请输入页码数" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="军事信息资源分类号" prop="miresnum">
          <el-input v-model="form.miresnum" placeholder="请输入军事信息资源分类号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="书名" prop="title">
          <el-input v-model="form.title" placeholder="请输入书名" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="版本信息" prop="versioninfor">
          <el-input v-model="form.versioninfor" placeholder="请输入版本信息" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="出版信息" prop="publishinginfor">
          <el-input v-model="form.publishinginfor" placeholder="请输入出版信息" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="出版地" prop="publicationplace">
          <el-input v-model="form.publicationplace" placeholder="请输入出版地" style="width: 200px" />
        </el-form-item>
        <el-form-item label="出版者" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版者" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="主题词" prop="subjectwords">
          <el-input v-model="form.subjectwords" placeholder="请输入主题词" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="主要负责人" prop="mainresperson">
          <el-input v-model="form.mainresperson" placeholder="请输入主要负责人" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="权限声明" prop="perstatement">
          <el-input v-model="form.perstatement" placeholder="请输入权限声明" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="资源制作者" prop="resourceproducer">
          <el-input v-model="form.resourceproducer" placeholder="请输入资源制作者" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="正文语种" prop="textlanguage">
          <el-input v-model="form.textlanguage" placeholder="请输入正文语种" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="出版日期" prop="publicationdate">
          <el-input v-model="form.publicationdate" placeholder="请输入出版日期" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="摘要" prop="abstract">
          <el-input v-model="form.abstract" placeholder="请输入摘要" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="关键词1" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键词" style="width: 200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMetadata, getMetadata, delMetadata, addMetadata, updateMetadata, exportMetadata } from "@/api/rmgb/metadata";

export default {
  name: "Metadata",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人民工兵元数据表格数据
      metadataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysid: null,
        pagenum: null,
        miresnum: null,
        title: null,
        versionInfor: null,
        publishingInfor: null,
        publicationPlace: null,
        publisher: null,
        subjectWords: null,
        mainResPerson: null,
        perStatement: null,
        resourceProducer: null,
        textLanguage: null,
        publicationDate: null,
        abstracts: null,
        keyWord: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sysid: [
          { required: true, message: "唯一键ID1不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人民工兵元数据列表 */
    getList() {
      this.loading = true;
      listMetadata(this.queryParams).then(response => {
        this.metadataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sysid: null,
        pagenum: null,
        miresnum: null,
        clnum: null,
        disnum: null,
        docidentcode: null,
        chabstract: null,
        chkeywords: null,
        authorgan: null,
        author: null,
        sourcepub: null,
        sourceissue: null,
        sourceyear: null,
        sourcetitle: null,
        subtitle: null,
        title: null,
        enkeywords: null,
        enabstract: null,
        filecontent: null,
        versioninfor: null,
        publishinginfor: null,
        publicationplace: null,
        publisher: null,
        subjectwords: null,
        mainresperson: null,
        perstatement: null,
        resourceproducer: null,
        textlanguage: null,
        publicationdate: null,
        abstract: null,
        keyword: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sysid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人民工兵元数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sysid = row.sysid || this.ids
      getMetadata(sysid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人民工兵元数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sysid != null) {
            updateMetadata(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMetadata(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sysids = row.sysid || this.ids;
      this.$modal.confirm('是否确认删除人民工兵元数据编号为"' + sysids + '"的数据项？').then(function() {
        return delMetadata(sysids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有人民工兵元数据数据项？').then(() => {
        this.exportLoading = true;
        return exportMetadata(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
